package com.leo.blog.login;



import com.leo.blog.login.service.SelfUserDetailsService;
import com.leo.blog.util.CacheModel;
import com.leo.blog.util.CacheUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.*;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 自定义登录验证
 * @author Xiongx
 * @version 1.0
 * @date 2021/6/11 15:15
 * @since JDK 1.8
 */
@Component
public class UserAuthenticationProvider implements AuthenticationProvider {

    @Autowired
    private SelfUserDetailsService selfUserDetailsService;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;


    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpServletResponse resp = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        //会话id
        String sessionId=req.getParameter("sessionId");
        System.out.println(sessionId);
        String codeStr=req.getParameter("codeStr");
        Object obj= CacheUtils.getObject(sessionId);
        if(obj!=null){
            CacheModel cacheModel=(CacheModel)obj;
            String val=String.valueOf(cacheModel.getValue());
            if(!codeStr.equals(val)){
                throw new AuthenticationServiceException("验证码输入错误");
            }
        }else {
            throw new AuthenticationServiceException("验证码已过期");
        }

        // 获取表单输入中返回的用户名
        String userName = (String) authentication.getPrincipal();

        // 获取表单中输入的密码
        String password = (String) authentication.getCredentials();


        // 查询用户是否存在
        SelfUserEntity userInfo = selfUserDetailsService.loadUserByUsername(userName);
        if (userInfo == null) {
            throw new UsernameNotFoundException("用户名不存在");
        }
        // 我们还要判断密码是否正确，这里我们的密码使用BCryptPasswordEncoder进行加密的
        if (!new BCryptPasswordEncoder().matches(password, userInfo.getPassword())) {
            throw new BadCredentialsException("密码不正确");
        }
//        // 还可以加一些其他信息的判断，比如用户账号已停用等判断
//        if (userInfo.getStatus().equals("PROHIBIT")){
//            throw new LockedException("该用户已被冻结");
//        }

        // 角色集合
        Set<GrantedAuthority> authorities = new HashSet<>();

//        // 查询用户角色
//        List<SysRole> sysRoleEntityList = sysUserService.selectSysRoleByUserId(userInfo.getUserId());
//        for (SysRole sysRoleEntity: sysRoleEntityList){
//            authorities.add(new SimpleGrantedAuthority("ROLE_" + sysRoleEntity.getRoleno()));
//        }

        userInfo.setAuthorities(authorities);
        // 进行登录
        return new UsernamePasswordAuthenticationToken(userInfo, password, authorities);
    }
    @Override
    public boolean supports(Class<?> authentication) {
        return true;
    }
}
